package me.zk.practice1.s;

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 输入：s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
 * 输出："apple"
 * 输入：s = "abpcplea", dictionary = ["a","b","c"]
 * 输出："a"
 * @Author: zhang kai
 * @Date: 2023/05/08
 * @Description:
 */
public class S14 {
    public static void main(String[] args) {
        System.out.println(a("abce", Lists.newArrayList("abe","abc","monkey","plea")));
    }

    public static String findLongestWord(String s, List<String> dictionary) {
        String str = "";
        for (String v1: dictionary) {
            int i = 0,j = 0;
            while (i < s.length() && j<v1.length()) {
                if (s.charAt(i) == v1.charAt(j)) {
                    j++;
                }
                i++;

            }
            if (j == v1.length()){
                //最长的且比较字母序数最小的
                if (v1.length() > str.length() || (v1.length() == str.length() && v1.compareTo(str) < 0)) {
                    str = v1;
                }
            }
        }
        return str;
    }
    public static String a(String s, List<String>dictionary) {
        String res = "";
        for (String t : dictionary) {
            int i = 0, j = 0;
            while (i < t.length() && j < s.length()) {
                if (t.charAt(i) == s.charAt(j)) {
                    ++i;
                }
                ++j;
            }
            if (i == t.length()) {
                if (t.length() > res.length() || (t.length() == res.length() && t.compareTo(res) < 0)) {
                    res = t;
                }
            }
        }
        return res;
    }
}
